#include "hugeint.h"

HugeInteger::HugeInteger (long number)
{
	fill (integer, integer + INTS_SIZE, 0); // vc6 doesn't automatically fill array with 0s
    from_long (number);
}

HugeInteger::HugeInteger (const char *str)
{
	fill (integer, integer + INTS_SIZE, 0);
    from_string (str);
}

HugeInteger::~HugeInteger ()
{

}

HugeInteger HugeInteger::add (const HugeInteger & to_add)
{
    HugeInteger new_int (*this);
    size_t count, new_size = size ();  
	if (to_add.size () > new_size)
		new_size = to_add.size ();

    for (count = 0; count < new_size + 1; count++) {
        if (new_int[count] + to_add[count] < 10)
        {
            new_int[count] += to_add[count];
        } else {
            new_int[count] += to_add[count] - 10;
            new_int[count+1]++;
        }
    }

    return new_int;
}

short& HugeInteger::operator [] (int index)
{
    if (index > size () - 1)
        integer_size = index;
    return integer[index];
}

short HugeInteger::operator [] (int index) const
{
    if (index > size () - 1)
        integer_size = index;
    return integer[index];
}

size_t HugeInteger::size () const
{
    return integer_size;
}

bool HugeInteger::is_equal_to (const HugeInteger & n_int) const
{
	if (size () != n_int.size ())
		return false;

	for (size_t i = 0; i < size () + 1; i++)
		if ((*this)[i] != n_int[i])
			return false;

	return true;
}

bool HugeInteger::is_greater_than (const HugeInteger & n_int) const
{
	if (size () != n_int.size ())
		return size () > n_int.size ();

	for (size_t i = 0; i < size () + 1; i++)
		if ((*this)[size () - i] != n_int[size () - i])
			return (*this)[size () - i] > n_int[size () - i];
	
	return false;
}

void HugeInteger::output () const
{
    for (size_t j = size () - 1; j > 0; j--) {
        cout << integer[j];
    }

    cout << integer[0];
}

void HugeInteger::input (const char *str)
{
    from_string (str);
}

void HugeInteger::from_string (const char *str)
{
    long number = atol (str);
    from_long (number);
}

void HugeInteger::from_long (long number)
{
    size_t count = 0;
    integer_size = 0;
    while (number)
    {
        integer[count++] = number % 10;
        number /= 10;
        integer_size++;
    }
}
